package xin.sunzy.spring.controller;

import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xin.sunzy.spring.constant.Constants;
import xin.sunzy.spring.event.LoginEvent;
import xin.sunzy.spring.model.PageBean;
import xin.sunzy.spring.model.User;
import xin.sunzy.spring.service.UserService;
import xin.sunzy.spring.util.ResponseUtils;
import xin.sunzy.spring.util.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Locale;

/**
 * @description: 用户登录
 * @email: <a href="sunzhenyu@terminus.io">孙振宇</a>
 * @author: ginkgo
 * @date: 2018/4/2
 */
@Slf4j
@RestController
@RequestMapping(value = "/open/user-login")
public class UserLoginController {

    @Autowired
    private EventBus eventBus;

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public PageBean login(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam String username,
            @RequestParam String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        List<User> list = userService.list(user);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseUtils.fail("username.or.password.incorrect");
        }
        User realUser = list.get(0);
        eventBus.post(new LoginEvent(realUser, request, response));
        return ResponseUtils.ok(realUser);
    }

    @GetMapping("/get-user")
    public PageBean getUser() {
        User user = UserUtils.getCurrentUser();
        if (null != user) {
            return ResponseUtils.ok(user);
        }
        return ResponseUtils.fail("user.out.of.login");
    }

    @GetMapping("/change-locale")
    public PageBean changeLocale(HttpServletRequest request, @RequestParam String locale) {
        Locale local = LocaleContextHolder.getLocale();
        if (locale.contains("en")) {
            local = Locale.US;
        } else if (locale.contains("zh")) {
            local = Locale.SIMPLIFIED_CHINESE;
        }
        request.getSession().setAttribute(Constants.USER_LOCALE, local);
        return ResponseUtils.ok(Boolean.TRUE);
    }

    @GetMapping("/logout")
    public PageBean logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.removeAttribute(Constants.SESSION_USER_ID);
        }
        return ResponseUtils.ok("user.logout.success", (Object[]) null);
    }
}
